﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HelpPanel : MonoBehaviour
{
    public Button Button_close;
    public Button Button_left;
    public Button Button_right;
    
    public GameObject content;
    public GameObject toggleGroup;
    
    public Image[] List_content;
    public Button[] List_toggleButton;
    
    int currentIndex = 0;
    
    void Start()
    {
        List_content = content.GetComponentsInChildren<Image>();
        List_toggleButton = toggleGroup.GetComponentsInChildren<Button>();
        CurrentIndex = 0;
        
        Refresh();
        AddEvent();
    }
    
    void AddEvent()
    {
        EventTriggerListener.Get(Button_close.gameObject, null).onClick = OnClick_Button_close;
        EventTriggerListener.Get(Button_left.gameObject, null).onClick = OnClick_Button_left;
        EventTriggerListener.Get(Button_right.gameObject, null).onClick = OnClick_Button_right;

        for (int i = 0; i < List_toggleButton.Length; i++)
        {
            var button = List_toggleButton[i];
            if (button != null)
            {
                EventTriggerListener.Get(button.gameObject, i).onClick = OnClick_Button_toggle;
            }
        }
    }

    void OnClick_Button_close(GameObject go, object data = null)
    {
        //NotifyEvent e = new NotifyEvent(NotifyType.END_LOGO, null, this);
        //NotifacitionCenter.GetInstance().postNotification(e);

        this.gameObject.SetActive(false);
    }

    void OnClick_Button_left(GameObject go, object data = null)
    {
        CurrentIndex--;
        Refresh();
    }

    void OnClick_Button_right(GameObject go, object data = null)
    {
        CurrentIndex++;
        Refresh();
    }

    void OnClick_Button_toggle(GameObject go, object data = null)
    {
        CurrentIndex = (int)data;
        Refresh();
    }
    
    void Refresh()
    {
        for (int i = 0; i < List_content.Length; i++)
        {
            var item = List_content[i];
            if(item != null)
            {
                item.gameObject.SetActive(false);
            }
        }

        var currentGo = List_content[CurrentIndex];
        if (currentGo != null)
        {
            currentGo.gameObject.SetActive(true);
        }

        //
        for (int i = 0; i < List_toggleButton.Length; i++)
        {
            var button = List_toggleButton[i];
            if (button != null)
            {
                var image = button.GetComponent<Image>();
                if (image != null)
                {
                    image.sprite = ResourceManager.dictionary_sprite[("Assets/Texture/Help/notselected_page.png").ToLower()];
                }
            }
        }

        var selectButton = List_toggleButton[CurrentIndex];
        if (selectButton != null)
        {
            var selectImage = selectButton.GetComponent<Image>();
            if (selectImage != null)
            {
                selectImage.sprite = ResourceManager.dictionary_sprite[("Assets/Texture/Help/selected_page.png").ToLower()];
            }
        }
    }

    public int CurrentIndex
    {
        get
        {
            return currentIndex;
        }
        set
        {
            if (value < 0)
            {
                value = 0;
            }
            if (value > List_content.Length - 1)
            {
                value = List_content.Length - 1;
            }

            currentIndex = value;
        }
    }
}